import api from '../../api'

// initial state
const state = {
  products: []
}

// getters
const getters = {}

// actions
const actions = {
  async getAllProducts ({ commit }, params) {
    const { data } = await api.product.productList(params)
    if (data.code === 200) {
      commit('setProducts', data.data)
    }
  }
}

// mutations
const mutations = {
  setProducts (state, products) {
    state.products = products
  },
  decrementProductInventory (state, { id }) {
    const product = state.products.find(product => product.id === id)
    product.inventory--
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}
